package com.yanggu.mapreduce.combiner;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;


public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {

    private Text text = new Text();

    private IntWritable intWritable = new IntWritable(1);


    /**
     * 需要重写父类Mapper的map方法
     *
     * @param key
     * @param value
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        //1. 获取一行
        String line = value.toString();
        //2. 分割一行字符串(通过空格进行分割)
        String[] values = line.split(" ");
        //3. 输出
        for (String word : values) {
            text.set(word);
            context.write(text, intWritable);
        }
    }
}
